package main.java.middle;

import java.util.HashMap;

/**
 * @Author: 郑国荣
 * @Date: 2023-08-17-11:47
 * @Description:
 */
public class LengthOfLongestSubstring {
        public static int lengthOfLongestSubstring(String s) {
            if (s.isEmpty()) return 0;
            int currentLen =0;
            int maxLen=0;
            int startIndex=0;
            int len = s.length();
            HashMap<Character, Integer> map = new HashMap<>();
            for (int endIndex = 0; endIndex < len; endIndex++) {
                char currentChar = s.charAt(endIndex);
                if (map.containsKey(currentChar)){
                    startIndex = Math.max(startIndex,map.get(currentChar)+1);
                }

                map.put(currentChar,endIndex);
                currentLen = endIndex-startIndex+1;
                maxLen = Math.max(currentLen,maxLen);
            }
            return maxLen;
        }



    public static void main(String[] args) {
        String s="aabdnghhadfghjklo";
        System.out.println(lengthOfLongestSubstring(s));
    }
}
